import Router from "@koa/router";
import { UserController } from "../controllers/user.controller";
import { validateBody, validateParams } from "../middleware/validation";
import {
  RegisterRequestSchema,
  UpdateUserRequestSchema,
  updatePasswordRequestSchema,
} from "../validators/user.validator";
import { AuthUtil } from "../utils/auth.util";
import { idSchema } from "../validators/request.validator";
import z from "zod";

const userRouter = new Router({
  prefix: "/api/users",
});

// 添加用户
userRouter.post(
  "/",
  AuthUtil.isAdmin(),
  validateBody(RegisterRequestSchema),
  UserController.register
);

// 修改用户密码
userRouter.patch(
  "/:id/password",
  AuthUtil.isNormal(),
  validateParams(idSchema),
  validateBody(updatePasswordRequestSchema),
  UserController.updatePassword
);

// 更新用户信息（需要管理员权限）
userRouter.put(
  "/:id",
  AuthUtil.isAdmin(),
  validateParams(idSchema),
  validateBody(UpdateUserRequestSchema),
  UserController.updateUser
);

// 禁用/启用用户（需要管理员权限）- 验证路由参数和请求体
userRouter.patch(
  "/:id/status",
  AuthUtil.isAdmin(),
  validateParams(idSchema),
  validateBody(z.object({ isDisable: z.boolean() })),
  UserController.updateUser
);

// 获取用户列表（需要管理员权限）
userRouter.get("/", AuthUtil.isAdmin(), UserController.listUsers);

// 删除用户
userRouter.delete(
  "/:id",
  AuthUtil.isAdmin(),
  validateParams(idSchema),
  UserController.deleteUser
);

export default userRouter;
